﻿Imports System.IO
Imports Ionic.Zip
Imports tukui.updater
Imports System.Net


Public Class core
    '    Private Function Update(ByVal WoWpath As String, ByVal mod_dl As String, ByVal Over_config As Boolean)

    '        'Local Variables
    '        'Dim Backup As Boolean
    '        'Dim wc As New WebClient
    '        'Dim Extracted As String
    '        'Dim extfolder As String
    '        WoWpath = WoWpath & "\Interface\AddOns"
    '        'mod_dl = My.Settings.mod_dl
    '        'Over_config = My.Settings.over_config
    '        Return 0 'BackWorker.ReportProgress(0)

    '        'Save Config
    '        'If File.Exists(WoWpath & "\Tukui\config\config.lua") = True And My.Settings.over_config = False Then
    '        '    File.Copy(WoWpath & "\Tukui\config\config.lua", "config.lua", True)
    '        '    Backup = True
    '        'Else
    '        '    Backup = False
    '        'End If

    '        'Return 16 'BackWorker.ReportProgress(16)

    '        'Download update
    '        wc.DownloadFile(My.Settings.mod_dl, "update.zip")
    '        Return 33 'BackWorker.ReportProgress(33)

    '        'Clean Up old installation
    '        If IO.Directory.Exists(WoWpath & "\Tukui") Then
    '            Directory.Delete(WoWpath & "\Tukui", True)
    '        End If
    '        If IO.Directory.Exists(WoWpath & "\Tukui_dps_layout") Then
    '            Directory.Delete(WoWpath & "\Tukui_dps_layout", True)
    '        End If
    '        If IO.Directory.Exists(WoWpath & "\Tukui_heal_layout") Then
    '            Directory.Delete(WoWpath & "\Tukui_heal_layout", True)
    '        End If

    '        Return 50 'BackWorker.ReportProgress(50)

    '        'Unzip new one
    '        Using Zip1 As ZipFile = ZipFile.Read("update.zip")
    '            Dim Ze As ZipEntry
    '            For Each Ze In Zip1
    '                Ze.Extract(WoWpath, ExtractExistingFileAction.OverwriteSilently)
    '            Next
    '        End Using

    '        Return 66 'BackWorker.ReportProgress(66)

    '        Directory.GetFileSystemEntries(WoWpath, "*tukui*")


    '        Extracted = Directory.GetDirectories(WoWpath, "tukz-tukui-*")(0)
    '        extfolder = Extracted
    '        Directory.Move(extfolder & "\Tukui", WoWpath & "\Tukui")
    '        Directory.Move(extfolder & "\Tukui_heal_layout", WoWpath & "\Tukui_heal_layout")
    '        Directory.Move(extfolder & "\Tukui_dps_layout", WoWpath & "\Tukui_dps_layout")

    '        Directory.Delete(extfolder)

    '        Return 83 'BackWorker.ReportProgress(83)

    '        'Move Config.lua back.
    '        If Backup = True Then
    '            File.Copy("config.lua", WoWpath & "\TukUI\config\config.lua", True)
    '        End If

    '        Return 100 'BackWorker.ReportProgress(100)
    '    End Function

    Public Function Backup(ByVal WoWpath As String)
        Dim Backuped As Boolean
        If Directory.Exists(IO.Path.GetTempPath & "\TukuiUpdater") = False Then
            Directory.CreateDirectory(IO.Path.GetTempPath & "\TukuiUpdater")
        End If

        If File.Exists(WoWpath & "\Tukui\config\config.lua") = True Then
            File.Copy(WoWpath & "\Tukui\config\config.lua", IO.Path.GetTempPath & "\TukuiUpdater\config.lua", True)
            Backuped = True
        Else
            Backuped = False
        End If

        Return Backuped
    End Function

    Public Function Download(ByVal Mod_dl As String, ByVal WoWpath As String)
        Dim wc As New WebClient

        If Directory.Exists(IO.Path.GetTempPath & "\TukuiUpdater") = False Then
            Directory.CreateDirectory(IO.Path.GetTempPath & "\TukuiUpdater")
        End If
        wc.DownloadFile(Mod_dl, IO.Path.GetTempPath & "\TukuiUpdater\update.zip")

        Return 1
    End Function

    Public Function Clean(ByVal WoWpath As String)
        If IO.Directory.Exists(WoWpath & "\Tukui") Then
            Directory.Delete(WoWpath & "\Tukui", True)
        End If
        If IO.Directory.Exists(WoWpath & "\Tukui_dps_layout") Then
            Directory.Delete(WoWpath & "\Tukui_dps_layout", True)
        End If
        If IO.Directory.Exists(WoWpath & "\Tukui_heal_layout") Then
            Directory.Delete(WoWpath & "\Tukui_heal_layout", True)
        End If
        Return 1
    End Function

    Public Function ProcessFiles(ByVal WoWpath As String, ByVal config_saved As Boolean)
        'Unzip new one
        Using Zip1 As ZipFile = ZipFile.Read(IO.Path.GetTempPath & "\TukuiUpdater\update.zip")
            Dim Ze As ZipEntry
            For Each Ze In Zip1
                Ze.Extract(WoWpath, ExtractExistingFileAction.OverwriteSilently)
            Next
        End Using

        'Directory.GetFileSystemEntries(WoWpath, "*tukui*")

        Dim Extracted As String
        Dim Extfolder As String
        Extracted = Directory.GetDirectories(WoWpath, "*-tukui-*")(0)
        Extfolder = Extracted
        Directory.Move(Extfolder & "\Tukui", WoWpath & "\Tukui")
        Directory.Move(Extfolder & "\Tukui_heal_layout", WoWpath & "\Tukui_heal_layout")
        Directory.Move(Extfolder & "\Tukui_dps_layout", WoWpath & "\Tukui_dps_layout")
        If Directory.Exists(Extfolder & "\Tukui_ConfigUI") = True Then
            Directory.Move(Extfolder & "\Tukui_ConfigUI", WoWpath & "\Tukui_ConfigUI")
        End If

        Directory.Delete(Extfolder)

        'Move Config.lua back.
        If config_saved = True Then
            File.Copy(IO.Path.GetTempPath & "\TukuiUpdater\config.lua", WoWpath & "\TukUI\config\config.lua", True)
        End If

        Return 1
    End Function
End Class
